package cn.itcast.travel.web.servlet.mood;

import cn.itcast.travel.domain.mood;
import cn.itcast.travel.service.MoodService;
import cn.itcast.travel.service.impl.MoodServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "Getmood", value = "/Getmood")
public class Getmood extends HttpServlet {
    MoodService service=new MoodServiceImpl();
    //获取心情日志
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id=request.getParameter("id");
        List<mood> ms=service.Getmood(id);
        ObjectMapper ob=new ObjectMapper();
        ob.writeValue(response.getWriter(),ms);
    }

    //添加心情
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map mp=request.getParameterMap();
        mood m=new mood();
        try {
            BeanUtils.populate(m,mp);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        service.addmood(m);
        ObjectMapper ob=new ObjectMapper();
        ob.writeValue(response.getWriter(),1);
    }
}
